package com.itheima.controller;

import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.Code;
import com.itheima.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangbin
 * @date 2022/6/6 15:05
 */
@RestController
@RequestMapping("/login")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有账号密码
     *
     * @return
     */
    @GetMapping("/selectAll")
    public List<User> selectAll() {
        List<User> users = userService.selectAll();
        return users;
    }

    /**
     * 查询账号密码是否存在
     */
    @PostMapping
    public Result selectUser(@RequestBody User user) {
        Integer result = userService.selectUser(user.getUsername(), user.getPassword());
        System.out.println(result);
        Integer code = result != null ? Code.GET_OK : Code.GET_ERR;
        String msg = result != null ? "查询成功" : "查询失败";
        return new Result(code, msg, null);

    }

    /**
     * 用户注册
     */
    @PostMapping(value = "/add")
    public Integer add(@RequestBody User user) {
        return userService.add(user);
    }


}
